/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MultiFileChooser.java
 *
 * Created on 28-Apr-2011, 10:49:17 AM
 */
package balony;

import java.io.File;
import java.util.HashSet;
import javax.swing.JFileChooser;

/**
 *
 * @author Barry Young
 */
public class MultiFileChooser extends javax.swing.JFrame {

    Balony b;
    Long last = System.currentTimeMillis();

    /** Creates new form MultiFileChooser */
    public MultiFileChooser() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser() {

        };
        jCheckBox1 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);

        jFileChooser1.setMultiSelectionEnabled(true);
        jFileChooser1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jFileChooser1MouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jFileChooser1MouseReleased(evt);
            }
        });
        jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFileChooser1ActionPerformed(evt);
            }
        });
        jFileChooser1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jFileChooser1PropertyChange(evt);
            }
        });

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Auto-select all sets");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFileChooser1ActionPerformed
        System.out.println("Action performed: " + evt.getActionCommand());
        if (evt.getActionCommand().equals(JFileChooser.APPROVE_SELECTION)) {
            this.setVisible(false);
            b.loadAnalysisFiles(jFileChooser1.getSelectedFiles());
            this.dispose();
            return;
        }

        if (evt.getActionCommand().equals(JFileChooser.CANCEL_SELECTION)) {
            this.dispose();
        }
    }//GEN-LAST:event_jFileChooser1ActionPerformed

    private void jFileChooser1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jFileChooser1MouseClicked
    }//GEN-LAST:event_jFileChooser1MouseClicked

    private void jFileChooser1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jFileChooser1MouseReleased
    }//GEN-LAST:event_jFileChooser1MouseReleased

    private void jFileChooser1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jFileChooser1PropertyChange

        if (!jCheckBox1.isSelected()) {
            return;
        }

        if (evt.getPropertyName().equals(JFileChooser.SELECTED_FILES_CHANGED_PROPERTY)) {
            return;
        }

        if (evt.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
            System.out.println("In loop");

            HashSet<File> newFiles = new HashSet<File>();
            File[] files = jFileChooser1.getSelectedFiles();

            for (File f : files) {
                int i = f.getAbsolutePath().lastIndexOf("set");
                if (i > -1) {
                    String pre = f.getAbsolutePath().substring(0, i);
                    for (File ff : f.getParentFile().listFiles()) {
                        if (ff.getAbsolutePath().startsWith(pre)) {
                            newFiles.add(ff);
                        }
                    }
                }
            }

            if (newFiles.size() > 0) {
                jFileChooser1.setSelectedFiles(newFiles.toArray(new File[newFiles.size()]));
            }
        }

    }//GEN-LAST:event_jFileChooser1PropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MultiFileChooser().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    public javax.swing.JFileChooser jFileChooser1;
    // End of variables declaration//GEN-END:variables
}
